<?php

namespace App\Admin\Metrics\Home;

use App\Models\User;
use Dcat\Admin\Widgets\Metrics\Line;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;

class NewUsers extends Line
{
    /**
     * 初始化卡片内容
     *
     * @return void
     */
    protected function init()
    {
        parent::init();
        $this->title('<span style="color:#487BFF;">新增用户</span>');
        $this->dropdown([
            '1' => '一天内',
            '7' => '过去七天',
            '15' => '过去十五天',
            '30' => '过去三十天',
        ]);
    }

    /**
     * 处理请求
     *
     * @param Request $request
     *
     * @return mixed|void
     */
    public function handle(Request $request)
    {

        $option = $request->get('option',1);
//        // 卡片内容
        $this->withContent(User::query()->where('created_at', '>=', now()->subDays($option))->count());
//        // 图表数据
        $list =  User::query()->selectRaw('DATE_FORMAT(created_at,"%Y-%m-%d") as day')
        ->selectRaw('COUNT(*) as count')
        ->where('created_at', '>=', now()->subDays($option))
        ->groupBy('day')
        ->get()->toArray();
        $list = Arr::pluck($list,'count');
        $this->withChart($list);
    }

    /**
     * 设置图表数据.
     *
     * @param array $data
     *
     * @return $this
     */
    public function withChart(array $data)
    {
        return $this->chart([
            'series' => [
                [
                    'name' => $this->title,
                    'data' => $data,
                ],
            ],
        ]);
    }
    public function renderContent()
    {
        $this->appendHtmlAttribute('style', "background: linear-gradient(180deg, #DFEEFF 0%, #FFF 100%);");

        return parent::renderContent(); // TODO: Change the autogenerated stub
    }

    /**
     *
     * 设置卡片内容.
     *
     * @param string $content
     *
     * @return $this
     */
    public function withContent($content)
    {
        return $this->content(
            <<<HTML
<div class="d-flex justify-content-between align-items-center mt-1" style="margin-bottom: 16px;">
    <h2 class="ml-1 font-lg-1">{$content}</h2>
    <span class="mb-0 mr-1 text-80">{$this->title}</span>
</div>
HTML
        );
    }
}
